package com.jingan.nursinghome.regulatory.adapter;

import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.CreateMicrometerTableDailog;
import com.jingan.nursinghome.regulatory.dialog.ScoreDialog;
import com.jingan.nursinghome.regulatory.dialog.ZhengaiDialog;
import com.jingan.nursinghome.regulatory.view.RectificationView;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.iView;
import com.jingan.nursinghome.regulatory.view.triangleView;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableAdapter2 extends RecyclerView.Adapter<MicrometerTableAdapter2.MyViewHolder> {
    BaseActivity baseActivity;

    public MicrometerTableAdapter2(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
    }

    //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txt_title;
        iView txt_question;
        triangleView triangleView;
        SpannerView spanerScore;
        RectificationView rectifi;
        View ling;

        public MyViewHolder(View itemView) {
            super(itemView);
            txt_title = (TextView) itemView.findViewById(R.id.txt_title);
            txt_question = (iView) itemView.findViewById(R.id.txt_question);
            triangleView = (com.jingan.nursinghome.regulatory.view.triangleView) itemView.findViewById(R.id.triangleView);
            ProportionUtils.getInstance().adapterTextView(txt_title);
            spanerScore = (SpannerView) itemView.findViewById(R.id.spanerScore);
            ProportionUtils.getInstance().adapterView(spanerScore);
            rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
            ling = itemView.findViewById(R.id.ling);
            ProportionUtils.getInstance().adapterView(ling);
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (MicrometerTable.getInstance().getTopicsRc().get(position).getIndex() == null) {
            return;
        }
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.txt_question.getLayoutParams());
        layoutParams.leftToLeft = holder.itemView.findViewById(R.id.left).getId();
        if (MicrometerTable.getInstance().getTopicsRc().get(position).getIndex().trim().equals("0")) {
            holder.txt_title.setText(MicrometerTable.getInstance().getTopicsRc().get(position).getTitle());//标题
            holder.triangleView.setInfo(MicrometerTable.getInstance().getTopicsRc().get(position).getTitleIndex());//标题下标
            layoutParams.topToTop = holder.itemView.findViewById(R.id.top).getId();
            holder.ling.setVisibility(View.GONE);
        } else {
            layoutParams.topToTop = holder.itemView.findViewById(R.id.top3).getId();
            holder.txt_title.setText(null);
            holder.triangleView.setInfo(null);
            holder.ling.setVisibility(View.VISIBLE);
        }
        holder.txt_question.setLayoutParams(layoutParams);
        String i = MicrometerTable.getInstance().getTopicsRc().get(position).getCount();//题号
        if (i.length() == 1) {
            i = "0" + i;
        }
        holder.txt_question.setInfo(i, MicrometerTable.getInstance().getTopicsRc().get(position).getQuestion());//题目
        //holder.spanerScore.setInfo("  "+MicrometerTable.getInstance().getTopicsRc().get(position).getMaxScore() + "分");//分数
        String cScore = MicrometerTable.getInstance().getTopicsRc().get(position).getCscore();
        if (cScore == null || cScore.trim().equals("")) {
            cScore = MicrometerTable.getInstance().getTopicsRc().get(position).getMaxScore();
            holder.spanerScore.setIsscore(false);
        }else {
            holder.spanerScore.setIsscore(true);
        }
        holder.spanerScore.setInfoAndColor("  " + cScore + "分", Color.parseColor("#444444"));
        holder.spanerScore.setTextSize(ProportionUtils.getInstance().adapterInt(32));
        holder.spanerScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String cS=MicrometerTable.getInstance().getTopicsRc().get(position).getCscore();
                if (cS == null || cS.trim().equals("")) {
                    cS=MicrometerTable.getInstance().getTopicsRc().get(position).getMaxScore();
                }

                ScoreDialog.getInstance(baseActivity, Integer.valueOf(cS), Integer.valueOf(MicrometerTable.getInstance().getTopicsRc().get(position).getMaxScore()), new BaseListenner<Integer>() {
                    @Override
                    public void onResult(Integer result) {
                        holder.spanerScore.setIsscore(true);
                        holder.spanerScore.setInfoAndColor("  " + result + "分", Color.parseColor("#444444"));
                        MicrometerTable.getInstance().getTopicsRc().get(position).setCscore(result + "");

                        String c = MicrometerTable.getInstance().getTopicsRc().get(position).getCscore();
                        String m = MicrometerTable.getInstance().getTopicsRc().get(position).getMaxScore();
                        if ((c == null || c.trim().equals("")) || !c.trim().equals(m.trim())) {
                            //非满分状态。弹出是否整改的询问窗口。
                            AlertDialog.getInstance(baseActivity, false, true).setMession("是否要整改？").setPositive("确认", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlertDialog.dismisss();
                                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai()), new BaseListenner<Integer>() {
                                        @Override
                                        public void onResult(Integer result) {
                                            MicrometerTable.getInstance().getTopicsRc().get(position).setZhengai(result+"");
                                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("0")){
                                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                                            }
                                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("1")){
                                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                                            }
                                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("2")){
                                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                                            }
                                        }
                                    });
                                }
                            });
                        }

                    }
                });
            }
        });

        if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("0")){
            holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
        }
        if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("1")){
            holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
        }
        if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("2")){
            holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
        }
        holder.rectifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai()), new BaseListenner<Integer>() {
                        @Override
                        public void onResult(Integer result) {
                            MicrometerTable.getInstance().getTopicsRc().get(position).setZhengai(result+"");
                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("0")){
                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                            }
                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("1")){
                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                            }
                            if(MicrometerTable.getInstance().getTopicsRc().get(position).getZhengai().equals("2")){
                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                            }
                        }
                    });
                }
        });

        //高度調整
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(330) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();

        //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
    }

    @Override
    public int getItemCount() {
        return MicrometerTable.getInstance().getTopicsRc().size();
    }
}
